package net
{
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.Dictionary;
	
	import mx.managers.CursorManager;

	public class NetLoader
	{
		private var loader:URLLoader;
		private var completeHandler:Function;
		private var requestURL:String;
		
		private static var comFunList:Dictionary = new Dictionary();
		public static var errorFun:Function;
		public function NetLoader()
		{
			loader = new URLLoader();
			configureListeners(loader);
		}
		
		public function load(url:String,completeHandler:Function=null,param:Object=null):void
		{
			trace("load url: ",url)
			requestURL = url;
			if(completeHandler != null)
			{
				comFunList[url] = completeHandler;
			}
			
//			this.completeHandler = completeHandler;
			
			var request:URLRequest = new URLRequest(url);
			
			if(param != null)
			{
				request.method = URLRequestMethod.POST;
				var urlVars:URLVariables = new URLVariables();
				
				for(var k:String in param)
				{
					urlVars[k] = param[k];
				}
				
				request.data = urlVars;
			}
			
			try
			{
				loader.load(request);
			} catch (error:Error) {
				trace("Unable to load requested document.");
			}
		}
		
		private function loadCompleteHandler(e:Event):void
		{
//			var loader:URLLoader = URLLoader(e.target);
//			loader.removeEventListener(Event.COMPLETE, loadCompleteHandler);
//			trace("load data: " + loader.data);
			
			var data:Object = JSON.parse(loader.data);
			
			completeHandler = comFunList[requestURL] as Function;
			//			this.completeHandler = completeHandler
			if(completeHandler != null)
			{
				completeHandler(data);
				completeHandler = null;
			}
			//trace(data);
		}
		
		private function configureListeners(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(Event.COMPLETE, loadCompleteHandler);
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		
		private function openHandler(event:Event):void {
			trace("openHandler");
		}
		
		private function progressHandler(e:ProgressEvent):void {
//			trace("progressHandler loaded:" + e.bytesLoaded + " total: " + e.bytesTotal);
		}
		
		private function securityErrorHandler(e:SecurityErrorEvent):void
		{
			trace("securityErrorHandler");
			if(errorFun != null)
			{
				errorFun();
			}
		}
		
		private function httpStatusHandler(e:HTTPStatusEvent):void {
			trace("httpStatusHandler: " + e.status);
		}
		
		private function ioErrorHandler(e:IOErrorEvent):void
		{
			if(errorFun != null)
			{
				errorFun();
			}
			trace("ioErrorHandler");
		}
	}
}